﻿/********************************************************************************
    Copyright (C) Binod Nepal, Planet Earth Solutions Pvt. Ltd., Kathmandu.
	Released under the terms of the GNU General Public License, GPL, 
	as published by the Free Software Foundation, either version 3 
	of the License, or (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
***********************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Web.Security;

namespace MixNP.Web.Classifieds
{
    public partial class Play : System.Web.UI.Page
    {
        double itemId = double.MinValue;
        double nextItemId = double.MinValue;

        protected void WatchlistLinkButton_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(User.Identity.Name))
            {
                FormsAuthentication.RedirectToLoginPage();
            }
            else
            {
                this.itemId = Pes.Utility.Conversion.TryCastDouble(RouteData.Values["item-id"]);

                MixNP.BusinessLayer.Items.AddToWatchlist(User.Identity.Name, itemId);
                Pes.Utility.PageUtility.ExecuteJavaScript("addOtherFields", "jQuery(window).ready(function () { showMessage('This item is now added to your watchlist. You can later review it using the dashboard.'); });", this.Page);
                this.LoadItem();
            }
        }

        protected void WishlistLinkButton_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(User.Identity.Name))
            {
                FormsAuthentication.RedirectToLoginPage();
            }
            else
            {
                this.itemId = Pes.Utility.Conversion.TryCastDouble(RouteData.Values["item-id"]);

                MixNP.BusinessLayer.Items.AddToWishlist(User.Identity.Name, itemId);
                Pes.Utility.PageUtility.ExecuteJavaScript("addOtherFields", "jQuery(window).ready(function () { showMessage('This item is now added to your wishlist. You can later review it using the dashboard.'); });", this.Page);
                this.LoadItem();
            }

        }

        protected void NextLinkButton_Click(object sender, EventArgs e)
        {
            this.itemId = this.nextItemId;
            this.LoadItem();
            UpdatePanel1.Update();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.LoadItem();
            }
        }

        private void LoadItem()
        {
            StringBuilder html = new StringBuilder();
            string title = string.Empty;
            decimal price = decimal.MinValue;
            string details = string.Empty;
            string displayTitle = string.Empty;
            string nextItemTitle = string.Empty;
            int index = int.MinValue;

            if (itemId.Equals(double.MinValue))
            {
                itemId = Pes.Utility.Conversion.TryCastDouble(RouteData.Values["item-id"]);
            }

            if (itemId.Equals(double.MinValue)) { itemId = 0; }

            nextItemId = MixNP.BusinessLayer.Items.GetRandomItemId(itemId);

            if (itemId.Equals(0))
            {
                Response.Redirect("~/play/" + nextItemId.ToString() + ".mix");
            }

            using (System.Data.DataTable table = MixNP.BusinessLayer.Items.GetItem(itemId))
            {
                if (table.Rows.Count.Equals(1))
                {
                    System.Data.DataRow row = table.Rows[0];
                    title = row["Title"].ToString();
                    price = Pes.Utility.Conversion.TryCastDecimal(row["Price"]);
                    details = row["Details"].ToString();

                    if (price.Equals(0))
                    {
                        displayTitle = title;
                    }
                    else
                    {
                        displayTitle = title + " for Rs. " + price.ToString("G29");
                    }

                    TitleLiteral.Text = displayTitle;

                    if (details.Length > 500)
                    {
                        DescriptionLiteral.Text = details.Substring(0, 500) + " ...";
                    }
                    else
                    {
                        DescriptionLiteral.Text = details;
                    }
                }
            }

            using (System.Data.DataTable table = MixNP.BusinessLayer.Items.GetImages(itemId))
            {
                if (table.Rows.Count > 0)
                {
                    html.Append("<div style=\"width: 680px; margin: 0 auto;\">");
                    html.Append("<div style=\"width: 436px; float: left;\">");

                    index = 0;

                    foreach (System.Data.DataRow row in table.Rows)
                    {
                        string path = row["FilePath"].ToString();//
                        string thumbnail = "/Handlers/GetItemImage.ashx?Path=" + path + "&W=100&H=100";
                        string source = "/Handlers/GetItemImage.ashx?Path=" + path + "&W=420&H=420";

                        if (index.Equals(0))
                        {
                            html.Append("<img id=\"mainImage\" alt=\"" + displayTitle + "\" src=\"" + source + "\" style=\"float: left; margin: 4px; padding: 4px; border: 1px solid #DEDEDE;\" />");
                            html.Append("</div>");
                            html.Append("<div style=\"width: 240px; float: left;\">");
                        }

                        html.Append("<img src='" + thumbnail + "' alt=\"" + displayTitle + "\"  class='thumbnail' onmouseover=\"displayImage('mainImage', '" + source + "');\" />");

                        index++;
                        if (index.Equals(6))
                        {
                            break;
                        }
                    }

                    html.Append("</div>");
                    html.Append("</div>");
                    html.Append("<div style=\"clear: both;\">");
                    html.Append("</div>");

                    ImageLiteral.Text = html.ToString();
                }
            }


            ShowHyperLink1.NavigateUrl = MixNP.BusinessLayer.Items.GetItemUrl(itemId, this.Page);
            ShowHyperLink2.NavigateUrl = MixNP.BusinessLayer.Items.GetItemUrl(itemId, this.Page);

            nextItemTitle = MixNP.BusinessLayer.Items.GetTitle(nextItemId);
            NextLinkButton1.Attributes.Add("onclick", "window.history.pushState('" + itemId.ToString() + "', '" + nextItemTitle + "', '" + "/play/" + nextItemId.ToString() + ".mix" + "');document.title='" + nextItemTitle + "';");

            NotNowHyperLink2.NavigateUrl = "~/play/" + nextItemId.ToString() + ".mix";

            MixNP.BusinessLayer.Items.Hit(itemId);

            this.Page.Title = displayTitle;
            this.Page.MetaDescription = DescriptionLiteral.Text;
        }

    }
}